package com.example.foodsy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.foodsy.dto.AddressDTO;
import com.example.foodsy.dto.Result;
import com.example.foodsy.entity.SlauInfo;
import com.example.foodsy.entity.WholInfo;
import com.example.foodsy.service.WholInfoService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * <p>
 * 批发商产品批号信息表 前端控制器
 * </p>
 *
 * @author cgc
 * @since 2025-09-23
 */
@RestController
@RequestMapping("/whol-info")
@CrossOrigin
public class WholInfoController {
	@Autowired
	private WholInfoService wholInfoService;

	//保存
	@PostMapping("/addWholInfo")
	public Result addWholInfo(@RequestBody WholInfo wholInfo) {
		boolean save = wholInfoService.addWholInfo(wholInfo);
		return save ? Result.ok():Result.fail("保存失败");
	}

	//通过状态进行查询
	@GetMapping("/listByState/{state}/{wholId}")
	public Result listByState(@PathVariable Integer state ,@PathVariable Integer wholId){
		return Result.ok(wholInfoService.list(new QueryWrapper<WholInfo>().eq("state", state).eq("whol_id",wholId) ));
	}

	//根据sbId查询上游企业信息
	@GetMapping("/getSlauInfo/{sbId}")
	public Result getSlauInfo(@PathVariable Integer sbId){
		AddressDTO<SlauInfo> slauInfo = wholInfoService.getSlauInfo(sbId);
		return Result.ok(slauInfo);
	}
	@GetMapping("/getWholInfoByBatchId/{batchId}")
	public Result getWholInfoByBatchId(@PathVariable String batchId){
		return Result.ok(wholInfoService.getOne(new QueryWrapper<WholInfo>().eq("batch_id", batchId)));
	}

	//更新
	@PostMapping("/editWholInfo")
	public Result editWholInfo(@RequestBody WholInfo wholInfo){
		boolean b = wholInfoService.updateById(wholInfo);
		return b ? Result.ok():Result.fail("更新失败");
	}

	//返回给上游企业进行确认的信息
	@GetMapping("getWholInfoBystate/{state}")
	public Result getWholInfoBystate(@PathVariable Integer state){
		return Result.ok(wholInfoService.getWholInfoBystate(state));
	}

	//将待确认信息改为已确认
	@PostMapping("/updateState/{batchId}")
	public Result updateState(@PathVariable String batchId){
		WholInfo wholInfo = wholInfoService.getOne(new QueryWrapper<WholInfo>().eq("batch_id", batchId));
		wholInfo.setState(3);
		return wholInfoService.updateById(wholInfo)?Result.ok():Result.fail("修改失败");
	}


	//下架
	@PostMapping("/delete/{batchId}")
	public Result delete(@PathVariable String batchId){
		WholInfo wholInfo = wholInfoService.getOne(new QueryWrapper<WholInfo>().eq("batch_id", batchId));
		wholInfo.setState(4);
		return wholInfoService.updateById(wholInfo)?Result.ok():Result.fail("修改失败");
	}

	//根据wholId查询批次号
	@GetMapping("/getinfoByWholId/{wholId}")
	public Result getinfoByWholId(@PathVariable Integer wholId){
		return Result.ok(wholInfoService.list(new QueryWrapper<WholInfo>().eq("whol_id", wholId)));
	}


}
